package com.lin.setting.setting.type;

import cn.hutool.crypto.digest.MD5;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.springframework.util.StringUtils;

import java.nio.charset.Charset;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @作者: 林江
 * @创建时间: 2024/5/2
 * @功能: 自定义类型处理器
 */
@Slf4j
public class TestTypeHandler extends BaseTypeHandler<String> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        log.info("setNonNullParameter.............");
        if (StringUtils.hasText(parameter)) {
            // 加密
            ps.setString(i, MD5.create().digestHex(parameter, Charset.defaultCharset()));
        } else {
            ps.setString(i, parameter);
        }

    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        log.info("getNullableResult.............1");
        return rs.getString(columnName);
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        log.info("getNullableResult.............2");
        return rs.getString(columnIndex);
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        log.info("getNullableResult.............3");
        return cs.getString(columnIndex);
    }
}
